package cn.lsngo.manager;

import android.content.Context;

import org.greenrobot.greendao.database.Database;

import java.util.ArrayList;
import java.util.List;

import cn.lsngo.manager.greendao.gen.DaoMaster;
import cn.lsngo.manager.greendao.gen.DaoSession;

/**
 * @Title : 数据库帮助类
 * @date : 2021/5/19 14:56
 */
public class DbManager {
    private static DbManager mInstance;

    private DbManager() {
    }

    /**
     * 双重检测锁
     */
    public static DbManager getInstance() {
        if (mInstance == null) {
            synchronized (DbManager.class) {
                if (mInstance == null) {
                    mInstance = new DbManager();
                }
            }
        }
        return mInstance;
    }

    /**
     * 初始化GreenDao，直接在Application中进行初始化操作
     */
    public void initGreenDao(Context context, String userId) {
        // 外置数据库
//        DatabaseContext context = new DatabaseContext(context);
        SQLiteOpenHelper mSqliteOpenHelper = new SQLiteOpenHelper(context, userId + "_data.db", null);
        Database db = mSqliteOpenHelper.getWritableDb();
        DaoMaster daoMaster = new DaoMaster(db);
        //创建daosession
        daoSession = daoMaster.newSession();
    }

    private DaoSession daoSession;

    public DaoSession getDaoSession() {
        return daoSession;
    }
}
